/*
 *   Swing Explorer. Tool for developers exploring Java/Swing-based application internals. 
 * 	 Copyright (C) 2012, Maxim Zakharenkov
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *   
 */
package org.swingexplorer;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Point;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;


/**
 *
 * @author  Maxim Zakharenkov
 */
public class PNLComponentDetails extends javax.swing.JPanel {
    
    /** Creates new form ComponentDetails */
    public PNLComponentDetails() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblLayout = new javax.swing.JLabel();
        txtLayout = new javax.swing.JTextField();
        txtBorder = new javax.swing.JTextField();
        txtSize = new javax.swing.JTextField();
        txtLocation = new javax.swing.JTextField();
        lblLocation = new javax.swing.JLabel();
        lblSize = new javax.swing.JLabel();
        txtBorderInsets = new javax.swing.JTextField();
        lblBorderInsets = new javax.swing.JLabel();
        chbOpaque = new javax.swing.JCheckBox();
        chbVisible = new javax.swing.JCheckBox();
        lblBorder = new javax.swing.JLabel();

        lblLayout.setText("Layout:");

        txtLayout.setEditable(false);

        txtBorder.setEditable(false);

        txtSize.setEditable(false);

        txtLocation.setEditable(false);

        lblLocation.setText("Location:");

        lblSize.setText("Size:");

        txtBorderInsets.setEditable(false);

        lblBorderInsets.setText("Border Insets:");

        chbOpaque.setText("Opaque");
        chbOpaque.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        chbOpaque.setMargin(new java.awt.Insets(0, 0, 0, 0));

        chbVisible.setText("Visible");
        chbVisible.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        chbVisible.setMargin(new java.awt.Insets(0, 0, 0, 0));

        lblBorder.setText("Border:");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblLayout)
                    .add(lblBorder)
                    .add(lblBorderInsets)
                    .add(chbOpaque))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, chbVisible, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                .add(txtBorderInsets, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(lblSize)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(txtSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(lblLocation)
                                .add(4, 4, 4)))
                        .add(0, 0, 0)
                        .add(txtLocation, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 71, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(txtLayout, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, txtBorder, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblLayout)
                    .add(txtLayout, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblBorder)
                    .add(txtBorder, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblBorderInsets)
                    .add(lblLocation)
                    .add(txtBorderInsets, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(txtLocation, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(txtSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(lblSize))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(chbVisible, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(chbOpaque)))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox chbOpaque;
    private javax.swing.JCheckBox chbVisible;
    private javax.swing.JLabel lblBorder;
    private javax.swing.JLabel lblBorderInsets;
    private javax.swing.JLabel lblLayout;
    private javax.swing.JLabel lblLocation;
    private javax.swing.JLabel lblSize;
    private javax.swing.JTextField txtBorder;
    private javax.swing.JTextField txtBorderInsets;
    private javax.swing.JTextField txtLayout;
    private javax.swing.JTextField txtLocation;
    private javax.swing.JTextField txtSize;
    // End of variables declaration//GEN-END:variables
    
    MdlSwingExplorer model;
    ModelListener modelListener = new ModelListener();
    
    public void setModel(MdlSwingExplorer model) {

		if (model == this.model) {
			return;
		}

		if (this.model != null) {
			this.model.removePropertyChangeListener(modelListener);
		}
		if (model != null) {
			model.addPropertyChangeListener(modelListener);
		}
		this.model = model;
		repaint();
	}
    
    class ModelListener implements PropertyChangeListener {

		public void propertyChange(PropertyChangeEvent evt) {
			String propName = evt.getPropertyName();
			if (!"selectedComponents".equals(propName)) {
				return;
			}
			
			
			txtSize.setText("N/A");
			txtLocation.setText("N/A");
			txtBorder.setText("N/A");
			txtLayout.setText("N/A");
			txtBorderInsets.setText("N/A");
			chbOpaque.setSelected(false);
			chbVisible.setSelected(false);
			
			Component[] selComps= model.getSelectedComponents();
			if(selComps.length ==0) {
				return;
			}
			
			// show size
			 Component curComponent = selComps[0];
			 Dimension size = curComponent.getSize();
			 txtSize.setText("" + size.width + "," + size.height);
			 
			 // show location
			 Point loc = curComponent.getLocation();
			 Component displComp = model.getDisplayedComponent();
			 if(displComp != null) {
				 SwingUtilities.convertPoint(curComponent, loc, displComp);
			 	txtLocation.setText("" + loc.x + "," + loc.y);
			 	
			 	//is showing
				 chbVisible.setSelected(displComp.isShowing());
			 }
			 
			 // converting to JComponent if possible
			 JComponent jcomp = null;
			 if(curComponent instanceof JComponent) {
				 jcomp = (JComponent) curComponent; 
			 } else {
				 return;
			 }
			 
			 // get border class with insets
			 Border brd = jcomp.getBorder();
			 if(brd != null) {
				 txtBorder.setText(brd.getClass().getName());
				 
				 Insets ins = brd.getBorderInsets(curComponent);
				 if(ins != null) {
					String strInsets = "top="  + ins.top + ",left=" + ins.left + ",bottom=" + ins.bottom + ",right=" + ins.right + "";
				 	txtBorderInsets.setText(strInsets);
				 }
			 } else {
				 txtBorder.setText("null");
			 }
			 
			 
			 
			 // get layout class
			 LayoutManager layout = jcomp.getLayout();
			 if(layout != null) {
				 txtLayout.setText(layout.getClass().getName());
			 } else {
				 txtLayout.setText("null");
			 }
			 
			 // opacity
			 chbOpaque.setSelected(jcomp.isOpaque());			 
		}
	}
}

